<?php

namespace app\model;

use support\Db;
use support\Model;

class Game extends Model
{
    protected $table = 'games';
    protected $connection = 'mysql_admin';
    protected $primaryKey = 'id';
    public $timestamps = false;


    public function gameConfigList($package_id=null,$game_id=null){
        $build = $this->from($this->getTable().' as g')->rightJoin('packages as p','p.game_id','g.id');
        if($package_id){
            if(is_array($package_id)){
                $build = $build->wherein('p.id',$package_id);
            }else{
                $build = $build->where('p.id',$package_id);
            }

        }
        if($game_id){
            if(is_array($game_id)){
                $build = $build->wherein('g.id',$game_id);
            }else{
                $build = $build->where('g.id',$game_id);
            }

        }
        $list = $build->select(DB::raw('p.id as package_id'),'p.game_id','g.cate_id','g.game_secret')->get()->toArray();
        return $list;
    }

    public function getGameConfig($package_id,$is_all=false){
        $list = $this->gameConfigList($package_id);
        $list = array_column($list,null,'package_id');
        if($is_all) return $list;
        return $list[$package_id] ?? [];
    }

    public function getGameConfigByGameId($game_id,$is_all=false){
        $list = $this->gameConfigList(0,$game_id);
        $list = array_column($list,null,'game_id');
        if($is_all) return $list;
        return $list[$game_id] ?? [];
    }


}